{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_svuu749"
   },
   "source": [
    "## Dictionary Example in Code\n",
    "\n",
    "In the cell below, we have created a hash table (`unordered_map`) to store the data from the example above. To create an `unordered_map` in C++, you must include the `<unordered_map>` header, and the sytnax for declaring an `unordered_map` is as follows:\n",
    "```cpp\n",
    "unordered_map <key_type, value_type> variable_name;\n",
    "```\n",
    "In the code below, we check if the `key` is in the `unordered_map` using the `.find()` method. If the key does not exist in the map, then `.find()` returns an `unordered_map::end()` type. Otherwise, `.find()` returns a [C++ iterator](http://www.cplusplus.com/reference/iterator/), which is a pointer that points to the beginning of the iterable key-value pair. \n",
    "\n",
    "We haven't covered iterators in this course, and you won't need them for this project, but they are a lot like pointers that can \"iterate\" forward or backward through a range."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "graffitiCellId": "id_u06vk9i",
    "graffitiConfig": {
     "executeCellViaGraffiti": "8w7cwxq_s5dyw48"
    }
   },
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "#include <vector>\n",
    "#include <unordered_map>\n",
    "#include <string>\n",
    "using std::vector;\n",
    "using std::cout;\n",
    "using std::unordered_map;\n",
    "using std::string;\n",
    "\n",
    "\n",
    "int main() {\n",
    "    // Create strings to use in the hash table.\n",
    "    string key = \"word\";\n",
    "    string def_1 = \"a unit of language, consisting of one or more spoken sounds or their written representation, that functions as a principal carrier of meaning\";\n",
    "    string def_2 = \"speech or talk: to express one's emotion in words\";\n",
    "    string def_3 = \"a short talk or conversation: 'Marston, I'd like a word with you.'\";\n",
    "    string def_4 = \"an expression or utterance: a word of warning\";\n",
    "    unordered_map <string, vector<string>> my_dictionary;\n",
    "\n",
    "    // Check if key is in the hash table.\n",
    "    if (my_dictionary.find(key) == my_dictionary.end()) {\n",
    "        cout << \"The key 'word' is not in the dictionary.\" << \"\\n\";\n",
    "        cout << \"Inserting a key-value pair into the dictionary.\" << \"\\n\\n\";\n",
    "        // Set the value for the key.\n",
    "        my_dictionary[key] = vector<string> {def_1, def_2, def_3, def_4};\n",
    "    }\n",
    "\n",
    "    // The key should now be in the hash table. You can access the\n",
    "    // value corresponding to the key with square brackets [].\n",
    "    // Here, the value my_dictionary[key] is a vector of strings.\n",
    "    // We iterate over the vector and print the strings.\n",
    "    cout << key << \": \\n\";\n",
    "    auto definitions = my_dictionary[key];\n",
    "    for (string definition : definitions) {\n",
    "        cout << definition << \"\\n\";\n",
    "    }\n",
    "}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_8w7cwxq"
   },
   "source": [
    "<span class=\"graffiti-highlight graffiti-id_8w7cwxq-id_s5dyw48\"><i></i><button>Compile & Execute</button></span>&nbsp;\n",
    "<span class=\"graffiti-highlight graffiti-id_ipxvjfh-id_p1f1z63\"><i></i><button>Explain</button></span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_sxuzijq",
    "graffitiConfig": {
     "rows": 12,
     "terminalId": "id_sxuzijq",
     "type": "terminal"
    }
   },
   "source": [
    "<i>Loading terminal (id_sxuzijq), please wait...</i>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_gevf04x"
   },
   "source": [
    "## Your Turn!\n",
    "\n",
    "The following is data relating some international country phone codes to their corresponding country name:\n",
    "```\n",
    "{{972, \"Israel\"}, {93, \"Afghanistan\"}, {355, \"Albania\"}, {213, \"Algeria\"}, {376, \"Andorra\"}, {244, \"Angola\"}, {54, \"Argentina\"}, {374, \"Armenia\"}, {297, \"Aruba\"}, {61, \"Australia\"}, {43, \"Austria\"}, {994, \"Azerbaijan\"}, {973, \"Bahrain\"}, {880, \"Bangladesh\"}, {375, \"Belarus\"}, {32, \"Belgium\"}, {501, \"Belize\"}, {229, \"Benin\"}, {975, \"Bhutan\"}, {387, \"Bosnia and Herzegovina\"}, {267, \"Botswana\"}, {55, \"Brazil\"}, {246, \"British Indian Ocean Territory\"}, {359, \"Bulgaria\"}, {226, \"Burkina Faso\"}, {257, \"Burundi\"}, {855, \"Cambodia\"}, {237, \"Cameroon\"}, {1, \"Canada\"}, {238, \"Cape Verde\"}, {236, \"Central African Republic\"}, {235, \"Chad\"}, {56, \"Chile\"}, {86, \"China\"}, {61, \"Christmas Island\"}, {57, \"Colombia\"}, {269, \"Comoros\"}, {242, \"Congo\"}, {682, \"Cook Islands\"}, {506, \"Costa Rica\"}, {385, \"Croatia\"}, {53, \"Cuba\"}, {537, \"Cyprus\"}, {420, \"Czech Republic\"}, {45, \"Denmark\"}, {253, \"Djibouti\"}, {593, \"Ecuador\"}, {20, \"Egypt\"}, {503, \"El Salvador\"}, {240, \"Equatorial Guinea\"}, {291, \"Eritrea\"}, {372, \"Estonia\"}, {251, \"Ethiopia\"}, {298, \"Faroe Islands\"}, {679, \"Fiji\"}, {358, \"Finland\"}, {33, \"France\"}, {594, \"French Guiana\"}, {689, \"French Polynesia\"}, {241, \"Gabon\"}, {220, \"Gambia\"}, {995, \"Georgia\"}, {49, \"Germany\"}, {233, \"Ghana\"}, {350, \"Gibraltar\"}, {30, \"Greece\"}, {299, \"Greenland\"}, {590, \"Guadeloupe\"}, {502, \"Guatemala\"}, {224, \"Guinea\"}, {245, \"Guinea-Bissau\"}, {595, \"Guyana\"}, {509, \"Haiti\"}, {504, \"Honduras\"}, {36, \"Hungary\"}, {354, \"Iceland\"}, {91, \"India\"}, {62, \"Indonesia\"}, {964, \"Iraq\"}, {353, \"Ireland\"}, {972, \"Israel\"}, {39, \"Italy\"}, {81, \"Japan\"}, {962, \"Jordan\"}, {254, \"Kenya\"}, {686, \"Kiribati\"}, {965, \"Kuwait\"}, {996, \"Kyrgyzstan\"}, {371, \"Latvia\"}, {961, \"Lebanon\"}, {266, \"Lesotho\"}, {231, \"Liberia\"}, {423, \"Liechtenstein\"}, {370, \"Lithuania\"}, {352, \"Luxembourg\"}, {261, \"Madagascar\"}, {265, \"Malawi\"}, {60, \"Malaysia\"}, {223, \"Mali\"}, {356, \"Malta\"}, {692, \"Marshall Islands\"}, {596, \"Martinique\"}, {222, \"Mauritania\"}, {230, \"Mauritius\"}, {262, \"Mayotte\"}, {52, \"Mexico\"}, {377, \"Monaco\"}, {976, \"Mongolia\"}, {382, \"Montenegro\"}, {212, \"Morocco\"}, {95, \"Myanmar\"}, {264, \"Namibia\"}, {674, \"Nauru\"}, {977, \"Nepal\"}, {31, \"Netherlands\"}, {599, \"Netherlands Antilles\"}, {687, \"New Caledonia\"}, {64, \"New Zealand\"}, {505, \"Nicaragua\"}, {227, \"Niger\"}, {234, \"Nigeria\"}, {683, \"Niue\"}, {672, \"Norfolk Island\"}, {47, \"Norway\"}, {968, \"Oman\"}, {92, \"Pakistan\"}, {680, \"Palau\"}, {507, \"Panama\"}, {675, \"Papua New Guinea\"}, {595, \"Paraguay\"}, {51, \"Peru\"}, {63, \"Philippines\"}, {48, \"Poland\"}, {351, \"Portugal\"}, {974, \"Qatar\"}, {40, \"Romania\"}, {250, \"Rwanda\"}, {685, \"Samoa\"}, {378, \"San Marino\"}, {966, \"Saudi Arabia\"}, {221, \"Senegal\"}, {381, \"Serbia\"}, {248, \"Seychelles\"}, {232, \"Sierra Leone\"}, {65, \"Singapore\"}, {421, \"Slovakia\"}, {386, \"Slovenia\"}, {677, \"Solomon Islands\"}, {27, \"South Africa\"}, {500, \"South Georgia and the South Sandwich Islands\"}, {34, \"Spain\"}, {94, \"Sri Lanka\"}, {249, \"Sudan\"}, {597, \"Suriname\"}, {268, \"Swaziland\"}, {46, \"Sweden\"}, {41, \"Switzerland\"}, {992, \"Tajikistan\"}, {66, \"Thailand\"}, {228, \"Togo\"}, {690, \"Tokelau\"}, {676, \"Tonga\"}, {216, \"Tunisia\"}, {90, \"Turkey\"}, {993, \"Turkmenistan\"}, {688, \"Tuvalu\"}, {256, \"Uganda\"}, {380, \"Ukraine\"}, {971, \"United Arab Emirates\"}, {44, \"United Kingdom\"}, {1, \"United States\"}, {598, \"Uruguay\"}, {998, \"Uzbekistan\"}, {678, \"Vanuatu\"}, {681, \"Wallis and Futuna\"}, {967, \"Yemen\"}, {260, \"Zambia\"}, {263, \"Zimbabwe\"}, {591, \"Bolivia, Plurinational State of\"}, {673, \"Brunei Darussalam\"}, {61, \"Cocos (Keeling) Islands\"}, {243, \"Congo, The Democratic Republic of the\"}, {225, \"Cote dIvoire\"}, {500, \"Falkland Islands (Malvinas)\"}, {44, \"Guernsey\"}, {379, \"Holy See (Vatican City State)\"}, {852, \"Hong Kong\"}, {98, \"Iran, Islamic Republic of\"}, {44, \"Isle of Man\"}, {44, \"Jersey\"}, {850, \"Korea, Democratic People's Republic of\"}, {82, \"Korea, Republic of\"}, {856, \"Lao People's Democratic Republic\"}, {218, \"Libyan Arab Jamahiriya\"}, {853, \"Macao\"}, {389, \"Macedonia, The Former Yugoslav Republic of\"}, {691, \"Micronesia, Federated States of\"}, {373, \"Moldova, Republic of\"}, {258, \"Mozambique\"}, {970, \"Palestinian Territory, Occupied\"}, {872, \"Pitcairn\"}, {262, \"Réunion\"}, {7, \"Russia\"}, {590, \"Saint Barthélemy\"}, {290, \"Saint Helena, Ascension and Tristan Da Cunha\"}, {590, \"Saint Martin\"}, {508, \"Saint Pierre and Miquelon\"}, {239, \"Sao Tome and Principe\"}, {252, \"Somalia\"}, {47, \"Svalbard and Jan Mayen\"}, {963, \"Syrian Arab Republic\"}, {886, \"Taiwan, Province of China\"}, {255, \"Tanzania, United Republic of\"}, {670, \"Timor-Leste\"}, {58, \"Venezuela, Bolivarian Republic of\"}, {84, \"Viet Nam\"}}\n",
    "```\n",
    "\n",
    "You can create an `unordered_map` with the data above just by pasting it into your program and assigning it to the appropriate variable. For example, an `unordered_map` can be declared and initialized as follows:\n",
    "```cpp\n",
    " std::unordered_map<int, std::string> mymap {\n",
    "     {5, \"a\"},\n",
    "     {6, \"b\"},\n",
    "     {7, \"c\"} \n",
    " };\n",
    "```\n",
    "\n",
    "#### Instructions\n",
    "- Write a C++ program that creates an `unordered_map` to store the data above. \n",
    "- Write some code to test if the 960 country code is in the data. If it is not, add the key/pair `{960, \"Maldives\"}` to the `unordered_map`.\n",
    "- Print out the value associated with the key \"960\" once you have done that to check that it is there."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "graffitiCellId": "id_ctvo7k9",
    "graffitiConfig": {
     "executeCellViaGraffiti": "m4bop9a_tacmric"
    }
   },
   "outputs": [],
   "source": [
    "// Write your program here.\n",
    "#include <unordered_map>\n",
    "#include <string>\n",
    "#include <iostream>\n",
    "using std::unordered_multimap;\n",
    "using std::string;\n",
    "using std::cout;\n",
    "\n",
    "int main()\n",
    "{\n",
    "    unordered_multimap<int, string> IDD_codes {{972, \"Israel\"}, {93, \"Afghanistan\"}, {355, \"Albania\"}, {213, \"Algeria\"}, {376, \"Andorra\"}, {244, \"Angola\"}, {54, \"Argentina\"}, {374, \"Armenia\"}, {297, \"Aruba\"}, {61, \"Australia\"}, {43, \"Austria\"}, {994, \"Azerbaijan\"}, {973, \"Bahrain\"}, {880, \"Bangladesh\"}, {375, \"Belarus\"}, {32, \"Belgium\"}, {501, \"Belize\"}, {229, \"Benin\"}, {975, \"Bhutan\"}, {387, \"Bosnia and Herzegovina\"}, {267, \"Botswana\"}, {55, \"Brazil\"}, {246, \"British Indian Ocean Territory\"}, {359, \"Bulgaria\"}, {226, \"Burkina Faso\"}, {257, \"Burundi\"}, {855, \"Cambodia\"}, {237, \"Cameroon\"}, {1, \"Canada\"}, {238, \"Cape Verde\"}, {236, \"Central African Republic\"}, {235, \"Chad\"}, {56, \"Chile\"}, {86, \"China\"}, {61, \"Christmas Island\"}, {57, \"Colombia\"}, {269, \"Comoros\"}, {242, \"Congo\"}, {682, \"Cook Islands\"}, {506, \"Costa Rica\"}, {385, \"Croatia\"}, {53, \"Cuba\"}, {537, \"Cyprus\"}, {420, \"Czech Republic\"}, {45, \"Denmark\"}, {253, \"Djibouti\"}, {593, \"Ecuador\"}, {20, \"Egypt\"}, {503, \"El Salvador\"}, {240, \"Equatorial Guinea\"}, {291, \"Eritrea\"}, {372, \"Estonia\"}, {251, \"Ethiopia\"}, {298, \"Faroe Islands\"}, {679, \"Fiji\"}, {358, \"Finland\"}, {33, \"France\"}, {594, \"French Guiana\"}, {689, \"French Polynesia\"}, {241, \"Gabon\"}, {220, \"Gambia\"}, {995, \"Georgia\"}, {49, \"Germany\"}, {233, \"Ghana\"}, {350, \"Gibraltar\"}, {30, \"Greece\"}, {299, \"Greenland\"}, {590, \"Guadeloupe\"}, {502, \"Guatemala\"}, {224, \"Guinea\"}, {245, \"Guinea-Bissau\"}, {595, \"Guyana\"}, {509, \"Haiti\"}, {504, \"Honduras\"}, {36, \"Hungary\"}, {354, \"Iceland\"}, {91, \"India\"}, {62, \"Indonesia\"}, {964, \"Iraq\"}, {353, \"Ireland\"}, {972, \"Israel\"}, {39, \"Italy\"}, {81, \"Japan\"}, {962, \"Jordan\"}, {254, \"Kenya\"}, {686, \"Kiribati\"}, {965, \"Kuwait\"}, {996, \"Kyrgyzstan\"}, {371, \"Latvia\"}, {961, \"Lebanon\"}, {266, \"Lesotho\"}, {231, \"Liberia\"}, {423, \"Liechtenstein\"}, {370, \"Lithuania\"}, {352, \"Luxembourg\"}, {261, \"Madagascar\"}, {265, \"Malawi\"}, {60, \"Malaysia\"}, {223, \"Mali\"}, {356, \"Malta\"}, {692, \"Marshall Islands\"}, {596, \"Martinique\"}, {222, \"Mauritania\"}, {230, \"Mauritius\"}, {262, \"Mayotte\"}, {52, \"Mexico\"}, {377, \"Monaco\"}, {976, \"Mongolia\"}, {382, \"Montenegro\"}, {212, \"Morocco\"}, {95, \"Myanmar\"}, {264, \"Namibia\"}, {674, \"Nauru\"}, {977, \"Nepal\"}, {31, \"Netherlands\"}, {599, \"Netherlands Antilles\"}, {687, \"New Caledonia\"}, {64, \"New Zealand\"}, {505, \"Nicaragua\"}, {227, \"Niger\"}, {234, \"Nigeria\"}, {683, \"Niue\"}, {672, \"Norfolk Island\"}, {47, \"Norway\"}, {968, \"Oman\"}, {92, \"Pakistan\"}, {680, \"Palau\"}, {507, \"Panama\"}, {675, \"Papua New Guinea\"}, {595, \"Paraguay\"}, {51, \"Peru\"}, {63, \"Philippines\"}, {48, \"Poland\"}, {351, \"Portugal\"}, {974, \"Qatar\"}, {40, \"Romania\"}, {250, \"Rwanda\"}, {685, \"Samoa\"}, {378, \"San Marino\"}, {966, \"Saudi Arabia\"}, {221, \"Senegal\"}, {381, \"Serbia\"}, {248, \"Seychelles\"}, {232, \"Sierra Leone\"}, {65, \"Singapore\"}, {421, \"Slovakia\"}, {386, \"Slovenia\"}, {677, \"Solomon Islands\"}, {27, \"South Africa\"}, {500, \"South Georgia and the South Sandwich Islands\"}, {34, \"Spain\"}, {94, \"Sri Lanka\"}, {249, \"Sudan\"}, {597, \"Suriname\"}, {268, \"Swaziland\"}, {46, \"Sweden\"}, {41, \"Switzerland\"}, {992, \"Tajikistan\"}, {66, \"Thailand\"}, {228, \"Togo\"}, {690, \"Tokelau\"}, {676, \"Tonga\"}, {216, \"Tunisia\"}, {90, \"Turkey\"}, {993, \"Turkmenistan\"}, {688, \"Tuvalu\"}, {256, \"Uganda\"}, {380, \"Ukraine\"}, {971, \"United Arab Emirates\"}, {44, \"United Kingdom\"}, {1, \"United States\"}, {598, \"Uruguay\"}, {998, \"Uzbekistan\"}, {678, \"Vanuatu\"}, {681, \"Wallis and Futuna\"}, {967, \"Yemen\"}, {260, \"Zambia\"}, {263, \"Zimbabwe\"}, {591, \"Bolivia, Plurinational State of\"}, {673, \"Brunei Darussalam\"}, {61, \"Cocos (Keeling) Islands\"}, {243, \"Congo, The Democratic Republic of the\"}, {225, \"Cote dIvoire\"}, {500, \"Falkland Islands (Malvinas)\"}, {44, \"Guernsey\"}, {379, \"Holy See (Vatican City State)\"}, {852, \"Hong Kong\"}, {98, \"Iran, Islamic Republic of\"}, {44, \"Isle of Man\"}, {44, \"Jersey\"}, {850, \"Korea, Democratic People's Republic of\"}, {82, \"Korea, Republic of\"}, {856, \"Lao People's Democratic Republic\"}, {218, \"Libyan Arab Jamahiriya\"}, {853, \"Macao\"}, {389, \"Macedonia, The Former Yugoslav Republic of\"}, {691, \"Micronesia, Federated States of\"}, {373, \"Moldova, Republic of\"}, {258, \"Mozambique\"}, {970, \"Palestinian Territory, Occupied\"}, {872, \"Pitcairn\"}, {262, \"Réunion\"}, {7, \"Russia\"}, {590, \"Saint Barthélemy\"}, {290, \"Saint Helena, Ascension and Tristan Da Cunha\"}, {590, \"Saint Martin\"}, {508, \"Saint Pierre and Miquelon\"}, {239, \"Sao Tome and Principe\"}, {252, \"Somalia\"}, {47, \"Svalbard and Jan Mayen\"}, {963, \"Syrian Arab Republic\"}, {886, \"Taiwan, Province of China\"}, {255, \"Tanzania, United Republic of\"}, {670, \"Timor-Leste\"}, {58, \"Venezuela, Bolivarian Republic of\"}, {84, \"Viet Nam\"}};\n",
    "    cout << IDD_codes.size() << \"\\n\";\n",
    "    auto range = IDD_codes.equal_range(1);\n",
    "    for (auto iterator = range.first; iterator != range.second; ++iterator)\n",
    "        cout << iterator->first << \": \" << iterator->second << \"\\n\";\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_m4bop9a"
   },
   "source": [
    "<span class=\"graffiti-highlight graffiti-id_m4bop9a-id_tacmric\"><i></i><button>Compile & Execute</button></span>&nbsp;\n",
    "<span class=\"graffiti-highlight graffiti-id_2mu7vl8-id_v76d204\"><i></i><button>See Solution</button></span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_8z9gwjg",
    "graffitiConfig": {
     "rows": 6,
     "terminalId": "id_epf697j",
     "type": "terminal"
    }
   },
   "source": [
    "<i>Loading terminal (id_8z9gwjg), please wait...</i>"
   ]
  }
 ],
 "metadata": {
  "graffiti": {
   "firstAuthorId": "813558546",
   "id": "id_z3y2v51",
   "language": "EN"
  },
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
   "name": "xeus-cling-cpp17"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "-std=c++17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
